<template>
  <a-modal
    title="转班确认"
    :width="500"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
        <a-form :form="form">
            <a-form-item
            style="display: none;"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            has-feedback
            >
            <a-input v-decorator="['childId']" />
            </a-form-item>
            <a-form-item
              label="原班级"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
                <span>{{detailInfo.className}}</span>
            </a-form-item>
            <a-form-item
              label="转入班级"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-select allowClear placeholder="请选择班级！" v-decorator="['classesId', {rules: [{required: true, message: '请选择班级！'}]}]">
                <a-select-option v-for="item in classList" :key="item.id" :value="item.id">{{item.name}}</a-select-option>
              </a-select>
            </a-form-item>
        </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { childrenShiftTransfers } from '@/api/modular/children/childManage'
  import moment from 'moment';
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 18 }
        },
        form: this.$form.createForm(this),
          detailInfo: {},
          classList: [],
          visible: false,
          confirmLoading: false,
      }
    },
    methods: {
      // 初始化方法
      show (record, classList) {
          this.detailInfo = record;
          this.classList = classList;
        this.visible = true;
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              childId: record.id
            }
          )
        }, 100)
      },
      handleSubmit(){
          
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            childrenShiftTransfers(values).then((res) => {
              if (res.success) {
                this.$message.success('转班成功')
                this.visible = false
                this.confirmLoading = false
                this.$emit('ok', values)
                this.form.resetFields()
              } else {
                this.$message.error('转班失败：' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.visible = false
      }
    }
  }
</script>
